package com.argticling.service;

import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.argticling.model.Article;
import com.argticling.model.User;

public class ArgticlingEmailSender {

	final String argticlingEmail = "argticling@gmail.com";
	final String argticlingEmailPassword = "testmail";
	final String emailSMTPserver = "smtp.gmail.com";
	final String emailServerPort = "465";
	final static String WEB_URL = "http://localhost:8080/";

	private User author;
	private Article article;
	private List<String> subscriptors;
	private String email_subject;
	private String email_body;

	public ArgticlingEmailSender(User author, Article article,
			List<String> subscriptors) {
		this.author = author;
		this.article = article;
		this.subscriptors = subscriptors;

		this.email_subject = getEmailSubject();
	}

	public void sendMailToSubscriptors() {

		for (String user : subscriptors) {
			this.email_body = getEmailContent(user);
			send(user);
		}
	}

	private boolean send(String email_to) {

		Properties props = new Properties();
		props.put("mail.smtp.user", argticlingEmail);
		props.put("mail.smtp.host", emailSMTPserver);
		props.put("mail.smtp.port", emailServerPort);
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.socketFactory.port", emailServerPort);
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");

		try {
			Authenticator auth = new SMTPAuthenticator();
			Session session = Session.getInstance(props, auth);

			MimeMessage msg = new MimeMessage(session);
			msg.setContent(email_body, "text/html");
			msg.setSubject(email_subject);
			msg.setFrom(new InternetAddress(argticlingEmail));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
					email_to));
			Transport.send(msg);
		} catch (Exception mex) {
			return false;
		}

		return true;
	}

	private String getEmailContent(String user) {

		StringBuffer sb = new StringBuffer();
		sb.append("<br />Un nuevo articulo de tu interes ha sido publicado en Articling.");
		sb.append("<br /><br />Autor: " + author.getName() + " " + author.getSurname());
		sb.append("<br />Titulo: " + article.getDc().getTitle());
		sb.append("<br />Resumen: " + article.getDc().getDescription());
		sb.append("<br /><br />Para leer el articulo haz click ");
		sb.append("<a href=\"" + WEB_URL + "article/read/" + article.getId()
				+ "\">aqui. </a>");
		sb.append("<br /><br />-----");
		sb.append("<br /><br />Este mail ha sido enviado automaticamente por Articling, ");
		sb.append("<br /><br />Para modificar su mail de subscripcion haga click ");
		sb.append("<a href=\"" + WEB_URL + "reader/modifyMail/"+ user +"."+ article.getId() +"\">aqui. </a>");
		sb.append("<br />Para dar de baja su mail de subscripcion haga click ");
		sb.append("<a href=\"" + WEB_URL + "reader/delete/" + user +"."+ article.getId()
				+ "\">aqui. </a>");
		sb.append("<br /><br />-----");

		return sb.toString();
	}

	private String getEmailSubject() {
		return "Publicacion Articling: " + article.getDc().getTitle();
	}

	private class SMTPAuthenticator extends javax.mail.Authenticator {
		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(argticlingEmail,
					argticlingEmailPassword);
		}
	}

}
